//
//  YTFishpondViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/1.
//

#import "YTFishpondViewController.h"
#import "AddFishpondShowView.h"
#import "AppendFishpondViewController.h"
#import "AppendMultipleFishpondViewController.h"
#import "YTFishpondHeaderView.h"
#import "fishpondStatisticsModel.h"
#import "YTFishpondSNTableViewCell.h"
#import "YTFishpondTKTableViewCell.h"
#import "foundQuestViewController.h"
#import "AppendDetailsViewController.h"
#import "AppendLogsViewController.h"
#import "PersonnelListViewController.h"
#import "ExamineViewController.h"
#import "AllQuestViewController.h"
#import "ClockViewController.h"
#import "WarnListViewController.h"
#import "AppenSelectViewController.h"
#import "MessageViewController.h"
#import "ScanQRcodeViewController.h"
#import "WeatherViewController.h"
#import "WeatherTool.h"




#import "btnClickViewController.h"

#import "CombinedPunchViewController.h"

@interface YTFishpondViewController ()<UITableViewDelegate>
///鱼塘名称
@property (nonatomic ,strong) UILabel * YTNamelab;
///报警消息按钮
@property (nonatomic ,strong) UIButton * BJBtn;
///报警消息数量
@property (nonatomic ,strong) UILabel * BJBtnCountlab;
///扫码按钮
@property (nonatomic ,strong) UIButton * SMBtn;
///天气温度
@property (nonatomic ,strong) UILabel * TQWDlab;
///待办红点
@property (nonatomic ,strong) UILabel * DBRedlab;
///任务红点
@property (nonatomic ,strong) UILabel * RWRedlab;
///审批红点
@property (nonatomic ,strong) UILabel * SPRedlab;
///交际红点
@property (nonatomic ,strong) UILabel * JJRedlab;
///tableviewModel
@property (nonatomic ,strong) fishpondOutdoorModel * tablemodel;
@property (nonatomic ,strong) NSArray <SYMessageListModel *>* messageArray;
@property (nonatomic ,strong) UIImageView * tqimg;

@end

@implementation YTFishpondViewController
- (void)setMessageArray:(NSArray<SYMessageListModel *> *)messageArray{
    _messageArray = messageArray;
    self.tablemodel.messageArray = messageArray;
    if(self.tablemodel.messageCount > 0){
        self.BJBtnCountlab.text = [NSString stringWithFormat:@"%d",self.tablemodel.messageCount];
        if(self.tablemodel.messageCount > 99){
            self.BJBtnCountlab.text = @"99+";
        }
        self.BJBtnCountlab.hidden = NO;
    } else {
        self.BJBtnCountlab.hidden = YES;
    }
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.NoDataHidden = YES;
    [self setupView];
//    
////    
//    CombinedPunchViewController * vc = [CombinedPunchViewController new];
//    vc.ID = @"1873766211717824513";
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupView{
    [[WeatherTool shared] openWeather];
    self.view.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(1, KSCREEN_HEIGHT) direction:GradientColorDirectionVertical startColor:KcolorHex(@"#005ADB") endColor:UIColor.whiteColor];
    UIView * topview = [UIView new];
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.left.offset(0);
        make.height.offset(KYiphoneX);
    }];
    MJWeakSelf
    UILabel * YTNamelab = [DTKTool labelWithText:@"养殖场" andFont:kFont(18) andTextColor:UIColor.whiteColor];
    self.YTNamelab = YTNamelab;
    [self.view addSubview:topview];
    [topview addSubview:YTNamelab];
    [YTNamelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(KTop/2);
    }];
    
    UIImageView * nameimg = kimageView(@"address_selection");
    [topview addSubview:nameimg];
    [nameimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(YTNamelab.mas_right).offset(kwt(10));
        make.centerY.equalTo(YTNamelab);
        make.width.height.offset(kwt(16));
    }];
    
    YTNamelab.addClickBlock(^{
        [weakSelf openBreedFarm];
    });
    nameimg.addClickBlock(^{
        [weakSelf openBreedFarm];
    });
    UIButton * BJBtn = [UIButton buttonWithType:0];
    BJBtn.addimg([UIImage imageNamed:@"news"]).addTarget(self,@selector(BJBtnClick));
    self.BJBtn = BJBtn;
    [topview addSubview:BJBtn];
    [BJBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.centerY.equalTo(YTNamelab);
        make.width.height.offset(kwt(26));
    }];
    
    UILabel * BJBtnCountlab = [DTKTool labelWithText:@"10" andFont:kFont(9) andTextColor:UIColor.whiteColor];
    BJBtnCountlab.textAlignment = NSTextAlignmentCenter;
    self.BJBtnCountlab = BJBtnCountlab;
    BJBtnCountlab.backgroundColor = KcolorHex(@"#F93A4A");
    [DTKTool YJBK:kwt(8) andView:BJBtnCountlab andWidth:kwt(1) andColor:KcolorHex(@"#005ADB")];
    [topview addSubview:BJBtnCountlab];
    [BJBtnCountlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(BJBtn.mas_right);
        make.centerY.equalTo(BJBtn.mas_top);
        make.height.offset(kwt(16));
        make.width.greaterThanOrEqualTo(@(kwt(16)));
    }];
    
    UIButton * SMBtn = [UIButton buttonWithType:0];
    SMBtn.addimg([UIImage imageNamed:@"scan"]).addTarget(self,@selector(SMBtnClick));
    self.SMBtn = SMBtn;
    [topview addSubview:SMBtn];
    [SMBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(BJBtn.mas_left).offset(kwt(-20));
        make.centerY.equalTo(YTNamelab);
        make.width.height.offset(kwt(26));
    }];
    
    UIView * topbtnview = [UIView new];
    [self.view addSubview:topbtnview];
    [topbtnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(kwt(80));
        make.top.equalTo(topview.mas_bottom);
    }];
    
    UIView * tqview = [UIView new];
    tqview.addClickBlock(^{
        [weakSelf TQViewClick];
    });
    tqview.addbackgroundColor(UIColor.whiteColor);
    [topbtnview addSubview:tqview];
    [DTKTool YJ:kwt(10) andView:tqview];
    [tqview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.width.height.offset(kwt(60));
        make.centerY.offset(0);
    }];
    
    UIImageView * tqimg = kimageView(@"");
    self.tqimg = tqimg;
    tqimg.contentMode = UIViewContentModeScaleAspectFit;
    [tqview addSubview:tqimg];
    [tqimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.width.offset(kwt(26));
        make.height.offset(kwt(26));
        make.centerX.offset(0);
    }];
    
    UILabel * TQWDlab = [DTKTool labelWithText:@"0°" andFont:kFont(11) andTextColor:KcolorHex(@"#424242")];
    self.TQWDlab = TQWDlab;
    TQWDlab.textAlignment = NSTextAlignmentCenter;
    [tqview addSubview:TQWDlab];
    [TQWDlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(41));
        make.width.offset(kwt(50));
        make.centerX.offset(0);
    }];
    
    
    
    UIView * toprightview = [UIView new];
    [DTKTool YJ:kwt(10) andView:toprightview];
    toprightview.addbackgroundColor(UIColor.whiteColor);
    [topbtnview addSubview:toprightview];
    [toprightview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tqview.mas_right).offset(kwt(8));
        make.height.offset(kwt(60));
        make.centerY.offset(0);
        make.right.offset(kwt(-10));
    }];
    
    UIView * dbview = [UIView new];
    dbview.addClickBlock(^{
        [weakSelf DBViewClick];
    });
    [toprightview addSubview:dbview];
    [dbview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
    }];
    
    UIImageView * dbimg = kimageView(@"tangkou_dealt_with");
    [dbview addSubview:dbimg];
    [dbimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.width.height.offset(kwt(26));
        make.centerX.offset(0);
    }];
    
    UILabel * DBRedlab = [DTKTool labelWithText:@"0" andFont:kFont(9) andTextColor:UIColor.whiteColor];
    [dbview addSubview:DBRedlab];
    self.DBRedlab = DBRedlab;
    DBRedlab.textAlignment = NSTextAlignmentCenter;
    [DTKTool YJ:kwt(7) andView:DBRedlab];
    DBRedlab.backgroundColor = KcolorHex(@"#F93A4A");
    [DBRedlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(14));
        make.width.greaterThanOrEqualTo(@(kwt(12)));
        make.centerX.equalTo(dbimg.mas_right);
        make.centerY.equalTo(dbimg.mas_top);
    }];
    
    UILabel * dblab = [DTKTool labelWithText:@"待办事项" andFont:kFont(11) andTextColor:KcolorHex(@"#424242")];
    [dbview addSubview:dblab];
    [dblab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(41));
        make.centerX.offset(0);
    }];
    
    
    UIView * RWview = [UIView new];
    RWview.addClickBlock(^{
        [weakSelf RWViewClick];
    });
    [toprightview addSubview:RWview];
    [RWview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(dbview.mas_right);
        make.width.equalTo(dbview);
    }];
    
    UIImageView * RDimg = kimageView(@"my_task");
    [RWview addSubview:RDimg];
    [RDimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.width.height.offset(kwt(26));
        make.centerX.offset(0);
    }];
    
    UILabel * RWRedlab = [DTKTool labelWithText:@"0" andFont:kFont(9) andTextColor:UIColor.whiteColor];
    [RWview addSubview:RWRedlab];
    self.RWRedlab = RWRedlab;
    RWRedlab.textAlignment = NSTextAlignmentCenter;
    [DTKTool YJ:kwt(7) andView:RWRedlab];
    RWRedlab.backgroundColor = KcolorHex(@"#F93A4A");
    [RWRedlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(14));
        make.centerX.equalTo(RDimg.mas_right);
        make.centerY.equalTo(RDimg.mas_top);
    }];
    
    UILabel * RWlab = [DTKTool labelWithText:@"我的任务" andFont:kFont(11) andTextColor:KcolorHex(@"#424242")];
    [RWview addSubview:RWlab];
    [RWlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(41));
        make.centerX.offset(0);
    }];
    
    UIView * SPview = [UIView new];
    SPview.addClickBlock(^{
        [weakSelf SPViewClick];
    });
    [toprightview addSubview:SPview];
    [SPview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(RWview.mas_right);
        make.width.equalTo(RWview);
    }];
    
    UIImageView * SPimg = kimageView(@"examine");
    [SPview addSubview:SPimg];
    [SPimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.width.height.offset(kwt(26));
        make.centerX.offset(0);
    }];
    
    UILabel * SPRedlab = [DTKTool labelWithText:@"0" andFont:kFont(9) andTextColor:UIColor.whiteColor];
    [SPview addSubview:SPRedlab];
    SPRedlab.textAlignment = NSTextAlignmentCenter;
    self.SPRedlab = SPRedlab;
    [DTKTool YJ:kwt(7) andView:SPRedlab];
    SPRedlab.backgroundColor = KcolorHex(@"#F93A4A");
    [SPRedlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(14));
        make.centerX.equalTo(SPimg.mas_right);
        make.centerY.equalTo(SPimg.mas_top);
    }];
    
    UILabel * SPlab = [DTKTool labelWithText:@"我的审批" andFont:kFont(11) andTextColor:KcolorHex(@"#424242")];
    [SPview addSubview:SPlab];
    [SPlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(41));
        make.centerX.offset(0);
    }];
    
    UIView * JJview = [UIView new];
    JJview.addClickBlock(^{
        [weakSelf JJViewClick];
    });
    [toprightview addSubview:JJview];
    [JJview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        make.left.equalTo(SPview.mas_right);
        make.width.equalTo(SPview);
    }];
    
    UIImageView * JJimg = kimageView(@"handover");
    [JJview addSubview:JJimg];
    [JJimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.width.height.offset(kwt(26));
        make.centerX.offset(0);
    }];
    
    UILabel * JJRedlab = [DTKTool labelWithText:@"0" andFont:kFont(9) andTextColor:UIColor.whiteColor];
    [JJview addSubview:JJRedlab];
    self.JJRedlab = JJRedlab;
    JJRedlab.textAlignment = NSTextAlignmentCenter;
    [DTKTool YJ:kwt(7) andView:JJRedlab];
    JJRedlab.backgroundColor = KcolorHex(@"#F93A4A");
    [JJRedlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(14));
        make.centerX.equalTo(JJimg.mas_right);
        make.centerY.equalTo(JJimg.mas_top);
    }];
    
    UILabel * JJlab = [DTKTool labelWithText:@"交接任务" andFont:kFont(11) andTextColor:KcolorHex(@"#424242")];
    [JJview addSubview:JJlab];
    [JJlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(41));
        make.centerX.offset(0);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.top.equalTo(topbtnview.mas_bottom);
    }];
    [self addHeaderView:^{
        [weakSelf loadNewData];
    }];
    UIView * xzrwview = [UIView new];
    xzrwview.addClickBlock(^{
        [weakSelf foundQuest];
    });
    xzrwview.backgroundColor = KcolorHex(@"#2A79FC");
    [DTKTool YJ:kwt(10) andView:xzrwview];
    [self.view addSubview:xzrwview];
    [xzrwview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.offset(kwt(-10));
        make.width.height.offset(kwt(44));
    }];
    
    UILabel * xzrwlab = [DTKTool labelWithText:@"新增\n任务" andFont:kFont(14) andTextColor:UIColor.whiteColor];
    xzrwlab.numberOfLines = 2;
    [xzrwview addSubview:xzrwlab];
    [xzrwlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
}
- (void)openBreedFarm{
    AppenSelectViewController * vc = [AppenSelectViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
   
}
- (void)foundQuest{
    foundQuestViewController * vc = [foundQuestViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)loadNewData{
    ///获取数据
    [SCNetManger PostHomeAppTaskCountWithDict:@{} andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code ==200){
            NSArray <appTaskCountModel *>* array = [NSArray yy_modelArrayWithClass:appTaskCountModel.class json:model.result ];
            for (appTaskCountModel * countModel in array) {
                if([countModel.typeName isEqualToString:@"代办事项"]){
                    if(countModel.count > 0){
                        self.DBRedlab.text = [NSString stringWithFormat:@"%d",countModel.count];
                        if(countModel.count > 99){
                            self.DBRedlab.text = @"...";
                        }
                        self.DBRedlab.hidden = NO;
                    } else {
                        self.DBRedlab.hidden = YES;
                    }
                }
                if([countModel.typeName isEqualToString:@"我的任务"]){
                    if(countModel.count > 0){
                        self.RWRedlab.text = [NSString stringWithFormat:@"%d",countModel.count];
                        self.RWRedlab.hidden = NO;
                        if(countModel.count > 99){
                            self.RWRedlab.text = @"...";
                        }
                    } else {
                        self.RWRedlab.hidden = YES;
                    }
                }
                if([countModel.typeName isEqualToString:@"我的审批"]){
                    if(countModel.count > 0){
                        self.SPRedlab.text = [NSString stringWithFormat:@"%d",countModel.count];
                        self.SPRedlab.hidden = NO;
                        if(countModel.count > 99){
                            self.SPRedlab.text = @"...";
                        }
                    } else {
                        self.SPRedlab.hidden = YES;
                    }
                }
                if([countModel.typeName isEqualToString:@"交接任务"]){
                    if(countModel.count > 0){
                        self.JJRedlab.text = [NSString stringWithFormat:@"%d",countModel.count];
                        self.JJRedlab.hidden = NO;
                        if(countModel.count > 99){
                            self.JJRedlab.text = @"...";
                        }
                    } else {
                        self.JJRedlab.hidden = YES;
                    }
                }
            }
            [self.tableview.mj_header endRefreshing];
            WeatherModel * Weathermodel = [WeatherModel shared];
            if( Weathermodel.dailyModel){
                self.tqimg.image = [UIImage imageNamed:Weathermodel.dailyModel.iconDay];
                self.TQWDlab.text = [NSString stringWithFormat:@"%@°~%@°",Weathermodel.dailyModel.tempMin,Weathermodel.dailyModel.tempMax ];
            }
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    ///当前养殖场
    [SCNetManger PostsysDepartQueryTreeBreedFarmWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code ==200){
            NSArray <PositionsModel *>* array = [NSArray yy_modelArrayWithClass:PositionsModel.class json:model.result ];
            for (PositionsModel * pondmodel in array) {
                if([pondmodel.value isEqualToString:[UserInfoModel shared].OrgCode]){
                    self.YTNamelab.text = pondmodel.label;
                }
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
    ///当前消息
    
    [SCNetManger PostsysSmsAppIndexSmsWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code ==200){
            NSArray <SYMessageListModel *>* array = [NSArray yy_modelArrayWithClass:SYMessageListModel.class json:model.result ];
            self.messageArray = array;
            WeatherModel * Weathermodel = [WeatherModel shared];
            if( Weathermodel.dailyModel){
                self.tqimg.image = [UIImage imageNamed:Weathermodel.dailyModel.iconDay];
                self.TQWDlab.text = [NSString stringWithFormat:@"%@°~%@°",Weathermodel.dailyModel.tempMin,Weathermodel.dailyModel.tempMax ];
            }
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];

    if(self.tablemodel.isWS){
        [self openSW];
    } else {
        if(self.tablemodel.name.length > 0){
            [self openSNWithID:self.tablemodel.ID andName:self.tablemodel.name];
        } else {
            [self openSN];
        }
    }
}
///室外
- (void)openSW{
    [self.view showBusyHUD];
    [SCNetManger PostHomeGetAppOutDoorPondStatisticsWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        fishpondOutdoorModel * m = [fishpondOutdoorModel yy_modelWithDictionary:model.result];
        m.isWS = YES;
        m.messageArray = self.messageArray;
        self.tablemodel = m;
        WeatherModel * Weathermodel = [WeatherModel shared];
        if( Weathermodel.dailyModel){
            self.tqimg.image = [UIImage imageNamed:Weathermodel.dailyModel.iconDay];
            self.TQWDlab.text = [NSString stringWithFormat:@"%@°~%@°",Weathermodel.dailyModel.tempMin,Weathermodel.dailyModel.tempMax ];
        }
        
        [self.tableview reloadData];
    }];
}
///室内
- (void)openSN{
    [self.view showBusyHUD];
    [SCNetManger PostGetAppTissueStatisticsWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        fishpondOutdoorModel * m = [fishpondOutdoorModel yy_modelWithDictionary:model.result];
        m.isWS = NO;
        m.messageArray = self.messageArray;
        self.tablemodel = m;
        WeatherModel * Weathermodel = [WeatherModel shared];
        if( Weathermodel.dailyModel){
            self.tqimg.image = [UIImage imageNamed:Weathermodel.dailyModel.iconDay];
            self.TQWDlab.text = [NSString stringWithFormat:@"%@°~%@°",Weathermodel.dailyModel.tempMin,Weathermodel.dailyModel.tempMax ];
        }
        [self.tableview reloadData];
    }];
}
///室内
- (void)openSNWithID:(NSString *)ID andName:(NSString *)name{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:ID forKey:@"id"];
    [SCNetManger PostHomeGetAppGreenHousePondStatisticsWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        fishpondOutdoorModel * m = [fishpondOutdoorModel yy_modelWithDictionary:model.result];
        m.isWS = NO;
        m.name = name;
        m.ID = ID;
        m.messageArray = self.messageArray;

        self.tablemodel = m;
        WeatherModel * Weathermodel = [WeatherModel shared];
        if( Weathermodel.dailyModel){
            self.tqimg.image = [UIImage imageNamed:Weathermodel.dailyModel.iconDay];
            self.TQWDlab.text = [NSString stringWithFormat:@"%@°~%@°",Weathermodel.dailyModel.tempMin,Weathermodel.dailyModel.tempMax ];
        }
        
        [self.tableview reloadData];
    }];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YTFishpondHeaderView * headerview = [YTFishpondHeaderView initview];
    headerview.model = self.tablemodel;
    MJWeakSelf
    headerview.openSNBlock = ^{
        [weakSelf openSN];
    };
    headerview.openSWBlock = ^{
        [weakSelf openSW];
    };
    headerview.openXZTKBlock = ^{
        [weakSelf openXZTK];
    };
    headerview.openMessageBlock = ^{
        [weakSelf BJBtnClick];
    };
    headerview.openYCBlock = ^(int alarmDegree) {
        WarnListViewController * vc = [WarnListViewController new];
        vc.alarmDegree = [NSString stringWithFormat:@"%d",alarmDegree];
        vc.tissueType = (weakSelf.tablemodel.isWS)?1:0;
        if(weakSelf.tablemodel.isWS == NO){
            vc.tissueId = weakSelf.tablemodel.ID;
        }
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    return headerview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(self.tablemodel.messageArray.count < 1){
        return kwt(100);
    }
    return kwt(185);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footview = [UIView new];
    footview.clipsToBounds = YES;
    UIView * backview = [UIView new];
    [DTKTool YJ:kwt(10) andView:backview];
    backview.backgroundColor = KcolorHex(@"#F2F7FF");
    [footview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.bottom.offset(kwt(-10));
        make.top.offset(kwt(-10));
    }];
   
    if(self.tablemodel.isWS == NO && self.tablemodel.name.length < 1){
        if(self.tablemodel.appPondTissueStatisticsListModelList.count < 1){
            
            UIImageView * noImage = [UIImageView new];
            [backview addSubview:noImage];
            noImage.image = [UIImage imageNamed:@"no_data"];
            [noImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(kwt(-50));
            }];
            
            UILabel * noTitleLab = [UILabel new];
            [backview addSubview:noTitleLab];
            noTitleLab.text = @"暂无数据";
            noTitleLab.font = kCFont(17);
            noTitleLab.textColor = [UIColor colorWithHexString:@"#262626"];
            [noTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(noImage.mas_bottom).offset(30);
                make.centerX.mas_offset(0);
            }];
        }
    } else {
        if(self.tablemodel.appPondManageListModelList.count < 1){
            UIImageView * noImage = [UIImageView new];
            [backview addSubview:noImage];
            noImage.image = [UIImage imageNamed:@"no_data"];
            [noImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(kwt(-50));
            }];
            
            UILabel * noTitleLab = [UILabel new];
            [backview addSubview:noTitleLab];
            noTitleLab.text = @"暂无数据";
            noTitleLab.font = kCFont(17);
            noTitleLab.textColor = [UIColor colorWithHexString:@"#262626"];
            [noTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(noImage.mas_bottom).offset(30);
                make.centerX.mas_offset(0);
            }];
        }
    }
    return footview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if(self.tablemodel.isWS == NO && self.tablemodel.name.length < 1){
        if(self.tablemodel.appPondTissueStatisticsListModelList.count < 1){
            return SCREEN_HEIGHT - KYiphoneX - KtabBar_HeightX - kwt(280);
        } else if(self.tablemodel.appPondTissueStatisticsListModelList.count < 4){
            ///1行
            return SCREEN_HEIGHT - KYiphoneX - KtabBar_HeightX - kwt(280 + 100);
        }
    } else {
        if(self.tablemodel.appPondManageListModelList.count < 1){
            return SCREEN_HEIGHT - KYiphoneX - KtabBar_HeightX - kwt(280);
        } else {
            if(SCREEN_HEIGHT - KYiphoneX - KtabBar_HeightX - kwt(120 * self.tablemodel.appPondManageListModelList.count + 280) > 0){
                return SCREEN_HEIGHT - KYiphoneX - KtabBar_HeightX - kwt(120 * self.tablemodel.appPondManageListModelList.count + 280);
            }
        }
    }
    
    return kwt(20);
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.tablemodel.isWS == NO && self.tablemodel.name.length < 1){
        return ceil(self.tablemodel.appPondTissueStatisticsListModelList.count / 3.0) ;
    } else {
        return self.tablemodel.appPondManageListModelList.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(self.tablemodel.isWS == NO && self.tablemodel.name.length < 1){
        return kwt(100);
    }
    return kwt(120);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    
    if(self.tablemodel.isWS == NO && self.tablemodel.name.length < 1){
        YTFishpondSNTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTFishpondSNTableViewCell"];
        if (cell == nil) {
            cell = [[YTFishpondSNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTFishpondSNTableViewCell"];
        }
        NSArray * modelarray = [self.tablemodel arrayWithRow:indexPath.row];
        if(modelarray.count > 0){
            cell.model1 = modelarray.firstObject;
        } else {
            cell.model1 = nil;
        }
        if(modelarray.count > 1){
            cell.model2 = modelarray[1];
        } else {
            cell.model2 = nil;
        }
        if(modelarray.count > 2){
            cell.model3 = modelarray.lastObject;
        } else {
            cell.model3 = nil;
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.openCJBlock = ^(appPondManageListModel * _Nonnull model) {
            [weakSelf openSNWithID:model.ID andName:model.name];
        };
        cell.addRWBlock = ^(appPondManageListModel * _Nonnull model) {
            foundQuestViewController * vc = [foundQuestViewController new];
//            vc.pondid = model.ID;
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell ;
    } else {
        YTFishpondTKTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTFishpondTKTableViewCell"];
        if (cell == nil) {
            cell = [[YTFishpondTKTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTFishpondTKTableViewCell"];
        }
        cell.model = self.tablemodel.appPondManageListModelList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.openTKBlock = ^(appPondManageListModel * _Nonnull model) {
            ///打开池塘
            [weakSelf openTkWithmodel:model];
        };
        cell.openDBBlock = ^(appPondManageListModel * _Nonnull model) {
            [weakSelf openDBWithmodel:model];
        };
        cell.openJRRWBlock = ^(appPondManageListModel * _Nonnull model) {
            [weakSelf openJRRWWithmodel:model];
        };
        cell.openRZBlock = ^(appPondManageListModel * _Nonnull model) {
            [weakSelf openRZWithmodel:model];
        };
        return cell ;
    }
}
///交接任务
- (void)JJViewClick{
    NSLog(@"点击交接");
    AllQuestViewController * vc = [AllQuestViewController new];
    vc.isJJ = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
///我的任务
- (void)RWViewClick{
    NSLog(@"点击任务");
    AllQuestViewController * vc = [AllQuestViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
///审批
- (void)SPViewClick{
    NSLog(@"点击审批");
    ExamineViewController * vc = [ExamineViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
///待办
- (void)DBViewClick{
    NSLog(@"点击代办");
    AllQuestViewController * vc = [AllQuestViewController new];
    vc.isDB = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
///天气点击
- (void)TQViewClick{
    NSLog(@"点击天气");
    [self addressWithBlock:^(NSString * _Nonnull str) {
        if(str.length < 1 || [str isEqualToString:@"未获取到位置信息"]){
            [self.view showWarning:@"无法获取天气信息"];
            return;
        }
        WeatherViewController * vc = [WeatherViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}
///报警按钮点击
- (void)BJBtnClick{
    MessageViewController * vc = [MessageViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

///扫描按钮点击
- (void)SMBtnClick{
    
    ScanQRcodeViewController * vc = [ScanQRcodeViewController new];
    MJWeakSelf
    vc.ScanBlock = ^(NSString * _Nonnull scanStr) {
        [weakSelf.view showWarning:scanStr];
        [weakSelf.navigationController popViewControllerAnimated:NO];
        AppendDetailsViewController * vc = [AppendDetailsViewController new];
        vc.pondId = scanStr;
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:NO];
    };
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
///新增池塘
- (void)openXZTK{
    MJWeakSelf
    [AddFishpondShowView showViewWith:^(BOOL isComplex) {
        if(isComplex){
            ///复数鱼塘
            AppendMultipleFishpondViewController * vc = [AppendMultipleFishpondViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else {
            ///单个鱼塘
            AppendFishpondViewController * vc = [AppendFishpondViewController new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    }];
}

- (void)openTkWithmodel:(appPondManageListModel *)model{
    AppendDetailsViewController * vc = [AppendDetailsViewController new];
    vc.pondId = model.ID;
    vc.titleStr = model.name;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)openDBWithmodel:(appPondManageListModel *)model{
    AllQuestViewController * vc = [AllQuestViewController new];
    vc.isDB = YES;
    vc.pondName = model.name;
    vc.pondId =model.ID;
    vc.xspondName = [NSString stringWithFormat:@"%@-%@",model.tissueName, model.name ];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)openJRRWWithmodel:(appPondManageListModel *)model{
    AllQuestViewController * vc = [AllQuestViewController new];
    vc.pondName = model.name;
    vc.pondId =model.ID;
    vc.xspondName = [NSString stringWithFormat:@"%@-%@",model.tissueName, model.name ];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)openRZWithmodel:(appPondManageListModel *)model{
    AppendLogsViewController * vc = [AppendLogsViewController new];
    vc.pondId = model.ID;
    vc.pondname = model.name;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
